/*
 * Copyright (c) 2006-2025, RT-Thread Development Team
 *
 * SPDX-License-Identifier: Apache-2.0
 *
 * Change Logs:
 * Date           Author       Notes
 * 2025-10-14     RT-Thread    ds3231 driver
 */

#ifndef __DRV_DS3231_H__
#define __DRV_DS3231_H__

#include <rtthread.h>
#include <rtdevice.h>

#define DS3231_I2C_BUS_NAME      "i2c2"
#define DS3231_I2C_ADDR          0x68

#define DS3231_REG_SECONDS       0x00
#define DS3231_REG_MINUTES       0x01
#define DS3231_REG_HOURS         0x02
#define DS3231_REG_DAY           0x03
#define DS3231_REG_DATE          0x04
#define DS3231_REG_MONTH         0x05
#define DS3231_REG_YEAR          0x06
#define DS3231_REG_CONTROL       0x0E
#define DS3231_REG_STATUS        0x0F
#define DS3231_REG_TEMP_MSB      0x11
#define DS3231_REG_TEMP_LSB      0x12

struct ds3231_time
{
    rt_uint8_t second;
    rt_uint8_t minute;
    rt_uint8_t hour;
    rt_uint8_t day;
    rt_uint8_t date;
    rt_uint8_t month;
    rt_uint8_t year;
};

struct ds3231_device
{
    struct rt_i2c_bus_device *i2c;
    rt_mutex_t lock;
};

typedef struct ds3231_device *ds3231_device_t;

ds3231_device_t ds3231_init(const char *i2c_bus_name);
void ds3231_deinit(ds3231_device_t dev);
rt_err_t ds3231_read_time(ds3231_device_t dev, struct ds3231_time *time);
rt_err_t ds3231_write_time(ds3231_device_t dev, struct ds3231_time *time);
rt_err_t ds3231_read_temperature(ds3231_device_t dev, float *temperature);

#endif
